package cloud.lysky.java.on.design_pattern.factory;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;

/**
 * @author luoly
 * @describe
 * @date 2022-08-20 13:46
 **/
public class LambdaProductFactory {

    private final static Map<String, Supplier<Product>> map = new HashMap<>();
    static {
        map.put("loan",Loan::new);
        map.put("stock",Stock::new);
        map.put("bond",Bond::new);
    }

    public static Product createProduct(String name){
        Supplier<Product> productSupplier = map.get(name);
        if (productSupplier != null){
            return productSupplier.get();
        }
        throw new IllegalArgumentException("No such product " + name);
    }
}